package com.newasia.management.controller;

import com.newasia.commons.result.ResponseData;
import com.newasia.commons.result.ResponseUtil;
import com.newasia.management.ISysRoleService;
import com.newasia.management.ISysUserService;
import com.newasia.management.dto.user.RoleUserRequest;
import com.newasia.management.dto.user.RoleUserResponse;
import com.newasia.management.dto.user.login.*;
import com.newasia.management.dto.user.user.DeleteSysUserRequset;
import com.newasia.management.dto.user.user.DeleteSysUserResponse;
import com.newasia.management.dto.user.user.SysUserListRequset;
import com.newasia.management.dto.user.user.SysUserListResponse;
import com.newasia.management.form.PageInfo;
import com.newasia.management.form.SysRoleForm;
import com.newasia.management.form.SysUserForm;
import com.newasia.management.shiro.SHA256Util;
import com.newasia.management.shiro.ShiroUtils;
import com.newasia.management.constants.ManagementRetCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/management")
@RequiresPermissions("sys:admin")
public class UserController {

    @Reference(timeout = 3000)
    ISysUserService sysUserService;

    @PostMapping("/user")
    public ResponseData register(@RequestBody SysUserForm form){
        String salt = new SecureRandomNumberGenerator().nextBytes().toHex(); //生成盐值
        String password = SHA256Util.sha256(form.getPassword(),salt);

        SysUserRegisterRequest request = new SysUserRegisterRequest();
        request.setUserName(form.getUserName());
        request.setUserPwd(password);
        request.setCompanyName(form.getCompanyName());
        request.setEmail(form.getEmail());
        request.setFile(form.getFile());
        request.setMobile(form.getMobile());
        request.setSalt(salt);
        request.setRoleId(form.getRoleId());
        SysUserRegisterResponse response = sysUserService.register(request);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }

    @PutMapping("/user/{uid}")
    public ResponseData updateUser(@RequestBody SysUserForm form,@PathVariable("uid") Long uid){
        String salt = new SecureRandomNumberGenerator().nextBytes().toHex(); //生成盐值
        String password = null;

        if(form.getPassword()!=null&&!"".equals(form.getPassword())){
            password = SHA256Util.sha256(form.getPassword(),salt);
        }

        SysUserUpdateRequest request = new SysUserUpdateRequest();
        request.setUserId(uid);
        request.setUserName(form.getUserName());
        request.setUserPwd(password);
        request.setCompanyName(form.getCompanyName());
        request.setEmail(form.getEmail());
        request.setFile(form.getFile());
        request.setState(form.getState());
        request.setMobile(form.getMobile());
        request.setSalt(salt);
        request.setRoleId(form.getRoleId());
        SysUserUpdateResponse response = sysUserService.updateUser(request);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }

    @GetMapping("/user")
    public ResponseData selectSysUserList(SysRoleForm form, PageInfo info){
        SysUserListRequset request = new SysUserListRequset();
        request.setRoleId(form.getRoleId());
        request.setUserName(form.getUserName());
        request.setPage(info.getPage());
        request.setSize(info.getSize());
        SysUserListResponse response = sysUserService.selectSysUserList(request);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }

    @DeleteMapping("/user/{uid}")
    public ResponseData deleteSysUser(@PathVariable("uid") Long uid){
        DeleteSysUserRequset request = new DeleteSysUserRequset();
        request.setUserId(uid);
        DeleteSysUserResponse response = sysUserService.deleteSysUser(request);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }
}
